/**
 * Class Views
 * This class manipulate HTMLElements
 * Example:
 *  //by html element css selector
 *  var collection = new Views("div");
 *  //by html NodeList
 *  var collection = new Views(document.getElementsByTagName("div"));
 */
Views = (function() {
    /**
     * View constructor
     * Create views collection based on selector or html collection
     * @param htmlcollection {String|HTMLCollection} css selector or html collection
     * @constructor
     */
    function Views(htmlcollection) {
        if (htmlcollection instanceof NodeList || htmlcollection instanceof HTMLCollection) {
            this.htmlcollection = htmlcollection;
            this.toViews();
        } else {
            this.getByCss(htmlcollection);
        }
    }
    /**
     * Get one view from views by index
     * @param index {Number} view index
     * @returns {View|*} view or undefined
     */
    Views.prototype.get = function(index) {
        return this.elements[index];
    };
    /**
     * Make html collection for views
     */
    Views.prototype.toViews = function() {
        var element, _i, _len, _ref;
        this.elements = [];
        _ref = this.htmlcollection;
        for (_i = 0, _len = _ref.length; _i < _len; _i++) {
            element = _ref[_i];
            this.elements.push(new View(element));
        }
    };
    /**
     * Get html collections and create views by tag name
     * @param tag {String} html tag name
     */
    Views.prototype.getByTag = function(tag) {
        this.htmlcollection = document.getElementsByTagName(tag);
        this.toViews();
    };
    /**
     * Get html collection and create views by css class
     * @param classname {String} css class name
     */
    Views.prototype.getByClass = function(classname) {
        this.htmlcollection = document.getElementsByClassName(classname);
        this.toViews();
    };
    /**
     * Get html collection and create views by css selector
     * @param selector {String} css selector
     */
    Views.prototype.getByCss = function(selector) {
        this.htmlcollection = document.querySelectorAll(selector);
        this.toViews();
    };
    /**
     * Add css class to all views
     * @param name {String} css class name
     */
    Views.prototype.addClass = function(name) {
        var element, _i, _len, _ref;
        _ref = this.elements;
        for (_i = 0, _len = _ref.length; _i < _len; _i++) {
            element = _ref[_i];
            element.addClass(name);
        }
    };
    /**
     * Remove css class from all views
     * @param name {String} css class name
     */
    Views.prototype.removeClass = function(name) {
        var element, _i, _len, _ref;
        _ref = this.elements;
        for (_i = 0, _len = _ref.length; _i < _len; _i++) {
            element = _ref[_i];
            element.removeClass(name);
        }
    };
    /**
     * For each views in this collection
     * @param fn {Function} for each callback
     */
    Views.prototype.forEach = function(fn){
        this.elements.forEach(fn);
    };

    return Views;

})();